package com.papers.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.papers.common.annotation.Log;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.excel.ExcelResult;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.system.domain.bo.TJuanMajorBo;
import com.papers.system.domain.dto.TJuanMajorDto;
import com.papers.system.domain.vo.TJuanLeftTreeVo;
import com.papers.system.domain.vo.TJuanMajorOutVo;
import com.papers.system.domain.vo.TJuanMajorPutVo;
import com.papers.system.domain.vo.TJuanMajorVo;
import com.papers.system.service.ITJuanMajorService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * 专业
 *
 * @author papers
 * @date 2022-10-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/juanMajor")
public class TJuanMajorController extends BaseController {

    private final ITJuanMajorService iTJuanMajorService;

    /**
     * 查询专业列表
     */
    @SaCheckPermission("system:juanMajor:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanMajorVo> list(TJuanMajorDto dto, PageQuery pageQuery) {
        return iTJuanMajorService.queryPageList(dto, pageQuery);
    }

    /**
     * 导出专业列表
     */
    @SaCheckPermission("system:juanMajor:export")
    @Log(title = "专业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanMajorBo bo, HttpServletResponse response) {
        //List<TJuanMajorVo> list = iTJuanMajorService.queryList(bo);
        //ExcelUtil.exportExcel(list, "专业", TJuanMajorVo.class, response);

        List<TJuanMajorPutVo> list = iTJuanMajorService.OutputList();
        ExcelUtil.exportExcel(list, "专业", TJuanMajorPutVo.class, response);
    }

    /**
     * 获取专业详细信息
     *
     * @param majorId 主键
     */
    @SaCheckPermission("system:juanMajor:query")
    @GetMapping("/{majorId}")
    public R<TJuanMajorVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable String majorId) {
        return R.ok(iTJuanMajorService.queryById(majorId));
    }

    /**
     * 新增专业
     */
    @SaCheckPermission("system:juanMajor:add")
    @Log(title = "专业", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanMajorBo bo) {
        //int i = iTJuanMajorService.MajorCodeUnique(bo);
        //if (i == 0) {
        //    return R.fail("新增专业失败，专业编号" + bo.getMajorCode() + "已存在");
        //}
        return toAjax(iTJuanMajorService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改专业
     */
    @SaCheckPermission("system:juanMajor:edit")
    @Log(title = "专业", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanMajorBo bo) {
        //int i = iTJuanMajorService.MajorCodeUnique(bo);
        //if (i == 0) {
        //    return R.fail("修改专业失败，专业编号" + bo.getMajorCode() + "已存在");
        //}
        return toAjax(iTJuanMajorService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除专业
     *
     * @param majorIds 主键串
     */
    @SaCheckPermission("system:juanMajor:remove")
    @Log(title = "专业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{majorIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] majorIds) {
        return toAjax(iTJuanMajorService.deleteWithValidByIds(Arrays.asList(majorIds), true) ? 1 : 0);
    }

    /**
     * 根据院系id查询专业列表
     * @param departmentId
     * @return
     */
    @GetMapping("/getMajorBydepartmentId")
    public R getMajorBydepartmentId(String departmentId) {
        return iTJuanMajorService.getMajorBydepartmentId(departmentId);
    }

    /**
     * 导入数据
     */
    @Log(title = "测试单表", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:juanMajor:import")
    @PostMapping("/importData")
    public R importData(@RequestPart("file") MultipartFile file) {
        InputStream fis = null;
        try {
            fis = file.getInputStream();
            ExcelResult<TJuanMajorOutVo> result = ExcelUtil.importExcel(fis, TJuanMajorOutVo.class, true);
            List<TJuanMajorOutVo> list = result.getList();
            if (list.size() == 0) {
                return R.fail("请先添加数据");
            }
            //保存
            return iTJuanMajorService.saveBatch(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 查询专业树
     *
     * @return
     */
    @GetMapping("/getDepartTree")
    public R<List<TJuanLeftTreeVo>> getDepartmentTree(String departId) {
        List<TJuanLeftTreeVo> majorTreeList = iTJuanMajorService.getDeptTreeList(departId);
        return R.ok(majorTreeList);
    }

}
